<?php

namespace app\admin\model\chase;

use think\Db;
use think\Model;

class GardenImages extends Model
{
    public static function createOrUpdate($garden_id, $images, $type)
    {
        $images = explode(',', $images);
        self::del($garden_id, $images, $type);
        if ($images[0]) {
            $data = [];
            foreach ($images as $image) {
                if (in_array($image, self::images($garden_id,$type))) {
                    continue ;
                }
                $data[] = [
                    'garden_id' => $garden_id,
                    'image' => $image,
                    'type' => $type
                ];
            }
            self::insertAll($data);
        }
    }

    public static function del($garden_id, $images, $type)
    {
        return self::where('garden_id', $garden_id)->where('type', $type)->whereNotIn('image', $images)->delete();
    }

    public static function images($garden_id, $type)
    {
        return self::where('garden_id', $garden_id)->where('type', $type)->column('image');
    }
}
